package com.wkbb.common.utils.excelUtil;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import lombok.extern.slf4j.Slf4j;

/**
 * String and string converter
 *
 * @author Jiaju Zhuang
 */
@Slf4j

public class CustomStringStringConverter implements Converter<String> {

    @Override
    public Class supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 这里读的时候会调用
     *
     * @param cellData
     *            NotNull
     * @param contentProperty
     *            Nullable
     * @param globalConfiguration
     *            NotNull
     * @return
     */
    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
        GlobalConfiguration globalConfiguration) {
        String value = null;
//        return "自定义：" + cellData.getStringValue();
        log.debug("解析到一列数据,数据类型:{}", cellData.getType());

        value = cellData.getStringValue();

        //把其他类型都转成字符串，防止因数据格式问题获取不到数据
        if(value == null){
            value = cellData.getNumberValue()==null?null:cellData.getNumberValue().toString();
        }

        //把其他类型都转成字符串，防止因数据格式问题获取不到数据
        if(value == null){
            value = cellData.getBooleanValue()==null?null:cellData.getBooleanValue().toString();
        }

//        if(value == null){ //公式暂不考虑
//            value = cellData.getFormulaValue()==null?null:cellData.getFormulaValue().toString();
//        }

        log.info("解析到一列数据,转换后为:{}", value);
        return value;
    }

    /**
     * 这里是写的时候会调用 不用管
     *
     * @param value
     *            NotNull
     * @param contentProperty
     *            Nullable
     * @param globalConfiguration
     *            NotNull
     * @return
     */
    @Override
    public CellData convertToExcelData(String value, ExcelContentProperty contentProperty,
        GlobalConfiguration globalConfiguration) {
        return new CellData(value);
    }

}
